export function timeDifference(date) {
    const now = new Date();
    const inputDate = new Date(date);

    const diffTime = now - inputDate;

    const diffMinutes = Math.floor(diffTime / (1000 * 60));
    const diffHours = Math.floor(diffTime / (1000 * 60 * 60));
    const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
    const diffWeeks = Math.floor(diffTime / (1000 * 60 * 60 * 24 * 7));
    const diffMonths = Math.floor(diffTime / (1000 * 60 * 60 * 24 * 30));

    const monthsDifference =
        (now.getFullYear() - inputDate.getFullYear()) * 12 +
        now.getMonth() -
        inputDate.getMonth();

    if (monthsDifference >= 12) {
        return `${inputDate.getFullYear()}-${String(
            inputDate.getMonth() + 1
        ).padStart(2, "0")}-${String(inputDate.getDate()).padStart(2, "0")}`;
    } else if (monthsDifference > 0) {
        return `${diffMonths}月前`;
    } else if (diffWeeks > 0) {
        return `${diffWeeks}周前`;
    } else if (diffDays > 0) {
        return `${diffDays}天前`;
    } else if (diffHours > 0) {
        return `${diffHours}小时前`;
    } else if (diffMinutes > 0) {
        return `${diffMinutes}分钟前`;
    } else {
        return "刚刚";
    }
}